package fr.jee.ejb.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.eclipse.persistence.annotations.Index;

@Entity
@Table(name = "profil")
@TableGenerator(name = "SEQUENCE_PROFIL", initialValue = 0, table = "sequence", pkColumnName = "code", valueColumnName = "valeur", allocationSize = 1)
public class Profil implements Serializable {

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQUENCE_PROFIL")
	private Integer id;

	@Index
	@Size(max = 100)
	@NotNull
	@Column(name = "libelle", length = 100, nullable = false)
	private String libelle;

	@NotNull
	@Column(name = "controle_total", nullable = false)
	private boolean controleTotal;

	@ManyToMany(cascade = CascadeType.REFRESH)
	@JoinTable(name = "profil_role", joinColumns = @JoinColumn(name = "ref_profil", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "ref_role", referencedColumnName = "code"))
	private List<Role> listeRole;

	public Profil() {
		super();
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLibelle() {
		return this.libelle;
	}

	public void setLibelle(String libelle) {
		this.libelle = libelle;
	}

	public boolean getControleTotal() {
		return this.controleTotal;
	}

	public void setControleTotal(boolean controleTotal) {
		this.controleTotal = controleTotal;
	}

	public List<Role> getListeRole() {
		if (listeRole == null) {
			listeRole = new ArrayList();
		}
		return listeRole;
	}

	public void setListeRole(List<Role> listeRole) {
		this.listeRole = listeRole;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		Profil other = (Profil) obj;
		return new EqualsBuilder().append(id, other.id).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(id).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).toString();
	}

}
